﻿namespace LogManager.Gui.ViewModels
{
    using LogManager.Core.Infrastructure.Logging;
    using LogManager.Core.Models;
    using LogManager.Core.Services;
    using LogManager.Core.Services.Location;
    using LogManager.Gui.Helpers;
    using LogManager.Gui.Views;
    using System.Threading.Tasks;
    using System.Windows.Input;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class LocationViewModel : ViewModelBase<ILocationView>, ILocationViewModel
    {
        public virtual ILocation Location { get; protected set; }
        public virtual SimpleServiceResult SourceCheckResult { get; protected set; }
        public virtual SimpleServiceResult DestinationCheckResult { get; protected set; }
        public virtual bool SourceCheckIsloading { get; protected set; }
        public virtual bool DestinationCheckIsloading { get; protected set; }
        private readonly ILocationService _checkLocationService;

        public LocationViewModel(ILocationView view, ILocation location, ILocationService checkLocationService)
            : base(view)
        {
            this.Location = location;
            this._checkLocationService = checkLocationService;
            this.Location.ParentServer.PropertyChanged += LocationParent_PropertyChanged;
            this.Location.ParentServer.ParentPlateform.PropertyChanged += LocationParent_PropertyChanged;
            this.Location.ParentServer.ParentPlateform.ParentWarehouse.PropertyChanged += LocationParent_PropertyChanged;
        }

        private void LocationParent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.RaisePropertyChanged(() => this.Location);
        }

        #region Commands

        private ICommand _deleteLocationCommand;
        public ICommand DeleteLocationCommand
        {
            get
            {
                _deleteLocationCommand = _deleteLocationCommand ?? new DelegateCommand(OnDeleteLocation);
                return _deleteLocationCommand;
            }
        }
        private void OnDeleteLocation()
        {
            this.Location.DeleteLocation();
            Log.Logger.AddInfo("Repertoire supprimé", this.Location.Name);
        }

        private AsyncDelegateCommand _checkSourceAccessCommand;
        public AsyncDelegateCommand CheckSourceAccessCommand
        {
            get
            {
                _checkSourceAccessCommand = _checkSourceAccessCommand ?? new AsyncDelegateCommand(OnCheckSourceAccess);
                return _checkSourceAccessCommand;
            }
        }
        private async Task OnCheckSourceAccess()
        {
            this.SourceCheckIsloading = true;
            this.SourceCheckResult = null;
            this.SourceCheckResult = await _checkLocationService.CheckLocationSourceAsync(this.Location);
            this.SourceCheckIsloading = false;

            if (SourceCheckResult.Succeeded)
                Log.Logger.AddSuccess("Test source ok. " + this.Location.FullPathSource, SourceCheckResult.Message);
            else
                Log.Logger.AddError("Test source échoué. " + this.Location.FullPathSource, SourceCheckResult.Message);
        }

        private AsyncDelegateCommand _checkDestinationAccessCommand;
        public AsyncDelegateCommand CheckDestinationAccessCommand
        {
            get
            {
                _checkDestinationAccessCommand = _checkDestinationAccessCommand ?? new AsyncDelegateCommand(OnCheckDestinationAccess);
                return _checkDestinationAccessCommand;
            }
        }
        private async Task OnCheckDestinationAccess()
        {
            this.DestinationCheckIsloading = true;
            this.DestinationCheckResult = null;
            this.DestinationCheckResult = await _checkLocationService.CheckLocationDestinationAsync(this.Location);
            this.DestinationCheckIsloading = false;

            if (DestinationCheckResult.Succeeded)
                Log.Logger.AddSuccess("Test destination ok. " + this.Location.FullPathDestination, DestinationCheckResult.Message);
            else
                Log.Logger.AddError("Test destination échoué. " + this.Location.FullPathDestination, DestinationCheckResult.Message);
        }

        ICommand ILocationViewModel.CheckSourceAccessCommand { get { return CheckSourceAccessCommand; } }
        ICommand ILocationViewModel.CheckDestinationAccessCommand { get { return CheckDestinationAccessCommand; } }

        #endregion
    }
}
